﻿using SanJ.FreeCare.Authorization.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Volo.Abp.Features;
using Volo.Abp.Identity;
using Volo.Abp;
using SanJ.FreeCare.Features;

public class UserPolicy : DomainService, IUserPolicy
{
    private readonly IFeatureChecker _featureChecker;
    private readonly IRepository<IdentityUser, Guid> _userRepository;

    public UserPolicy(IFeatureChecker featureChecker, IRepository<IdentityUser, Guid> userRepository)
    {
        _featureChecker = featureChecker;
        _userRepository = userRepository;
    }

    public async Task CheckMaxUserCountAsync(Guid tenantId)
    {
        var maxUserCount = (await _featureChecker.GetAsync<int>(AppFeatures.MaxUserCount)).To<int>();
        if (maxUserCount <= 0)
        {
            return;
        }

        var currentUserCount = await _userRepository.CountAsync();
        if (currentUserCount >= maxUserCount)
        {
            throw new UserFriendlyException("MaximumUserCount_Error_Message", "MaximumUserCount_Error_Detail");
        }
    }
}
